package com.icangya.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icangya.DAO.*;
import com.icangya.DAO.IAdminDAO;
import com.icangya.domain.*;
import com.icangya.service.IAdminUserService;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

import static com.icangya.Value.Error_Code.SUCCESS;
import static com.icangya.Value.Error_Code.UPDATE_ERROR_CODE;

@Service("AdminUserServiceImpl")
public class AdminUserServiceImpl implements IAdminUserService{

    @Autowired
    private ICarouselFigureDAO iCarouselFigureDAO;
    @Autowired
    private IProxyLevelDAO iProxyLevelDAO;
    @Autowired
    private IUserFootDAO iUserFootDAO;
    @Autowired
    private IBalanceDAO iBalanceDAO;
    @Autowired
    private ICommissionFootDAO iCommissionFootDAO;
    @Autowired
    private IEditionInfoDAO iEditionInfoDAO;
    @Autowired
    private IAdminDAO iAdminDAO;
    @Autowired
    private IKicKBallDAO iKicKBallDAO;
    @Autowired
    private IGiftAdvertMoneyDAO iGiftAdvertMoneyDAO;
    @Autowired
    private ISystemSettingDAO iSystemSettingDAO;
    @Autowired
    private ICommonProblemDAO iCommonProblemDAO;
    @Autowired
    private ISwitchDAO iSwitchDAO;
    @Autowired
    private IAliPaySwitchDAO iAliPaySwitchDAO;
    @Autowired
    private IFbTipDAO iFbTipDAO;
    @Autowired
    private ICustomerImageDAO iCustomerImageDAO;
    @Autowired
    private IFbFootGameDAO iFbFootGameDAO;

    @Override
    public Admin login(String name, String passWord, HttpSession session) {

        JSONObject jsonObject=new JSONObject();
        //判断参数是否为空
        if(StringUtils.isEmpty(name)|| StringUtils.isEmpty(passWord)){
            return null;
        }
        Admin admin = iAdminDAO.getUserByName(name);

        return admin;

    }

    @Override
    public Admin getAdmin(long id) {
       Admin admin= iAdminDAO.getAdminById(id);
        return admin;
    }

//修改代理用户名和密码
    @Override
    public JSONObject setAccountName(String name,String pwd,String pwd2) {
        JSONObject jsonObject=new JSONObject();

      int result=  iAdminDAO.checkAdminByPwd(name,pwd);
      if(result > 0){
           Admin admin=   iAdminDAO.getUserByName(name);
           admin.setPassWord(pwd2);
           boolean bool=iAdminDAO.update(admin);
           if(bool){
               jsonObject.put("msg","修改密码成功");
               jsonObject.put("error_code",SUCCESS);
           }
      }else{
          jsonObject.put("msg","输入原密码错误");
          jsonObject.put("error_code",UPDATE_ERROR_CODE);

      }

        return jsonObject;
    }

    @Override
    public JSONObject getAllCarouselFigure() {

        List list;
       list= iCarouselFigureDAO.getAllCarouselFigure();
        if(list==null){
            return null;
        }
       JSONArray jsonArray=new JSONArray();
        for (CarouselFigure c:(List<CarouselFigure>)list) {

            JSONObject jsonObject=new JSONObject();
            jsonObject.put("Id",c.getId());
            jsonObject.put("imgUrl",c.getImgUrl());
            jsonObject.put("imgContent",c.getImgContent());

            jsonArray.add(jsonObject);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("value",jsonArray);
        return jsonObject;
    }

    @Override
    public Boolean delCarouseByCid(long cid) {

      int result=  iCarouselFigureDAO.deleteByCarId(cid);

        if(result>0)
            return true;
        return false;
    }

    @Override
    public CarouselFigure setCarouselFigure(CarouselFigure carouselFigure) {


        return iCarouselFigureDAO.get( iCarouselFigureDAO.save(carouselFigure));
    }

    @Override
    public JSONObject getUserByProxy(int startPage,long proxyId) {
        List list;
        JSONObject json=new JSONObject();
        list=  iUserFootDAO.getAdminAllUser(startPage, proxyId);
        if(list==null)
            return null;// json.put("code","获取信息错误");
        JSONArray jsonArray=new JSONArray();
        try{
            for(Object[] obj:(List<Object[]>) list.get(0)){
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("id",obj[0]);
                jsonObject.put("openid",obj[1]);
                jsonObject.put("nickName",obj[2]);
                jsonObject.put("headImgUrl",obj[3]);
                jsonObject.put("createTime",obj[4]);
                jsonObject.put("agentName",iUserFootDAO.selectUserByUid(Long.valueOf(obj[6].toString())).getNickName());//用户上级代理名称
                jsonObject.put("parentId",obj[5]);//上限ID,推荐人
                jsonObject.put("money",iBalanceDAO.getBanlanceByOpenId(obj[1].toString()).getMoney());//用户广告金
                GiftAdvertMoney giftAdvertMoney= iGiftAdvertMoneyDAO.getGiftAdvertMoneyByOpenId(obj[1].toString());//赠送广告金不为null

                if(giftAdvertMoney ==null){
                    jsonObject.put("giftAdvertMoney",0);
                }else{
                    jsonObject.put("giftAdvertMoney",giftAdvertMoney.getGitAdvertMoney());
                }
                double comm=iCommissionFootDAO.selectUserCommis(obj[1].toString()).getMoney();//用户佣金
                BigDecimal big=new BigDecimal(comm);
                double commMoney=big.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                jsonObject.put("commMoney",commMoney);
                jsonArray.add(jsonObject);

            }
            json.put("totalPage",list.get(1));
            json.put("inPage",list.get(2));
            json.put("value",jsonArray);
        }catch (NullPointerException e){
            e.printStackTrace();
            return null;
        }
        return json;
//            for(User user:(List<User>) list.get(0)){
//                JSONObject jsonObject=new JSONObject();
//                jsonObject.put("id",user.getId());
//                jsonObject.put("openid",user.getOpenId());
//                jsonObject.put("nickName",user.getNickName());
//                jsonObject.put("headImgUrl",user.getHeadImgUrl());
//                jsonObject.put("createTime",user.getCreateTime());
//                jsonObject.put("agentName",iUserFootDAO.selectUserByUid(user.getProxyId()).getNickName());//用户上级代理名称
//                jsonObject.put("parentId",user.getParentId());//上限ID,推荐人
//                jsonObject.put("money",iBalanceDAO.getBanlanceByOpenId(user.getOpenId()).getMoney());//用户广告金
//                GiftAdvertMoney giftAdvertMoney= iGiftAdvertMoneyDAO.getGiftAdvertMoneyByOpenId(user.getOpenId());//赠送广告金不为null
//
//                if(giftAdvertMoney ==null){
//                    jsonObject.put("giftAdvertMoney",0);
//                }else{
//                    jsonObject.put("giftAdvertMoney",giftAdvertMoney.getGitAdvertMoney());
//                }
//
//                double comm=iCommissionFootDAO.selectUserCommis(user.getOpenId()).getMoney();//用户佣金
//                BigDecimal big=new BigDecimal(comm);
//                double commMoney=big.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//                jsonObject.put("commMoney",commMoney);
//                jsonArray.add(jsonObject);
//
//            }
//            json.put("totalPage",list.get(1));
//            json.put("inPage",list.get(2));
//            json.put("value",jsonArray);
//        }catch (NullPointerException e){
//            e.printStackTrace();
//            return null;
//        }
//        return json;
    }


    @Override
    public JSONObject checkUserById(long uid,long proxyId) {
//         User user=iUserFootDAO.selectUserByUid(uid);
        User user=iUserFootDAO.checkUserById(uid,proxyId);
         if(user==null)
             return null;
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",user.getId());
        jsonObject.put("openid",user.getOpenId());
        jsonObject.put("nickName",user.getNickName());
        jsonObject.put("headImgUrl",user.getHeadImgUrl());
        jsonObject.put("createTime",user.getCreateTime());
        jsonObject.put("money",iBalanceDAO.getBanlanceByOpenId(user.getOpenId()).getMoney());//用户广告金
        jsonObject.put("agentName",iUserFootDAO.selectUserByUid(user.getProxyId()).getNickName());//用户上级代理名称
        jsonObject.put("parentId",user.getParentId());//上限ID,推荐人
        jsonObject.put("commMoney",iCommissionFootDAO.selectUserCommis(user.getOpenId()).getMoney());//用户佣金
        GiftAdvertMoney giftAdvertMoney= iGiftAdvertMoneyDAO.getGiftAdvertMoneyByOpenId(user.getOpenId());//赠送广告金不为null
        jsonObject.put("giftAdvertMoney",0);
        if(giftAdvertMoney !=null)
            jsonObject.put("giftAdvertMoney",giftAdvertMoney.getGitAdvertMoney());

        jsonObject.put("count1",iProxyLevelDAO.getFirstCount(user.getId()));
        jsonObject.put("count2",iProxyLevelDAO.getSecondCount(user.getId()));
        jsonObject.put("count3",iProxyLevelDAO.getThirdCount(user.getId()));

        return jsonObject;
    }

    @Override
    public EditionInfo addEdition(EditionInfo editionInfo) {
        return iEditionInfoDAO.get(iEditionInfoDAO.save(editionInfo));
    }

    @Override
    public JSONObject getEditionAll() {

        List<EditionInfo> list=iEditionInfoDAO.getVersion();
        if(list==null)
            return null;

        JSONArray jsonArray=new JSONArray();
        for(EditionInfo editionInfo:list){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",editionInfo.getId());
            jsonObject.put("versionNum",editionInfo.getVersionNum());
            jsonObject.put("versionContent",editionInfo.getVersionContent());
            jsonObject.put("createTime",editionInfo.getCreateTime());
            jsonArray.add(jsonObject);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("value",jsonArray);
        return jsonObject;

    }
//添加客服图片
    @Override
    public CustomerImage saveCustomerImage(CustomerImage customerImage) {
        return iCustomerImageDAO.get(iCustomerImageDAO.save(customerImage));
    }
//获取客服图片
    @Override
    public CustomerImage getCustomerImage() {
        return iCustomerImageDAO.getCustomerImage();
    }

    //修改游戏比例设置
    @Override
    public FbFootGame updateFbFootGame(FbFootGame footGame) {
          boolean bool=  iFbFootGameDAO.update(footGame);
          if(bool)
              return iFbFootGameDAO.get(footGame.getId());
        return null;
    }

    //获取游戏比例设置
    @Override
    public FbFootGame getFbFootGame(int type) {

        return iFbFootGameDAO.getFbFootGameByType(type);
    }

    @Override
    public List<FbFootGame> getFbFootGameAll() {

        return iFbFootGameDAO.getFbFootGameAll();
    }

    @Override
    public JSONObject getKickBallByOpenId(String openId,int startPage) {
        List list=iKicKBallDAO.getKicBallByOpenId(openId,startPage);
        if(list==null)
        return null;

        JSONArray jsonArray=new JSONArray();

        for (KicKBall ball:(List<KicKBall>)list.get(0)) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("amountMoney",ball.getAmountMoney());
            jsonObject.put("kickballTime",ball.getKickBallTime());
            jsonObject.put("openId",ball.getOpenId());
            jsonObject.put("betAmount",ball.getBetAmount());
            jsonArray.add(jsonObject);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("value",jsonArray);
        jsonObject.put("totalPage",list.get(1));
        jsonObject.put("inPage",list.get(2));
        return jsonObject;
    }

//用户广告金记录
    //@Override
//    public JSONObject getAdvertMoney(String openId,int startPage) {
//
//        //用户发布广告的支出记录
//        List listEx=  iExpendRecordDAO.getExpendRecordByOpenId(openId);
//        // iExpendRecordDAO.getExpendByOpenIdOrType(openId, StaticValue.Advert);
//        JSONArray reArray=new JSONArray();
//        if(listEx!=null){
//            for (RechargeRecord r:(List<RechargeRecord>) listEx.get(0)) {
//                JSONObject jsonObject=new JSONObject();
//                jsonObject.put("openId",r.getOpenId());
//                jsonObject.put("money",r.getMoney());
//                jsonObject.put("rechargeTime",r.getRechargeTime());
//                jsonObject.put("status",r.getStatus());
//                reArray.add(jsonObject);
//
//            }
//        }
//
//        //用户充值的记录
//        List list= iRechargeRecordDAO.getRechargeRecordByOpenId(openId,startPage);
//        JSONArray erArray=new JSONArray();
//        if(list!=null){
//            for (ExpendRecord expendRecord:( List<ExpendRecord>)listEx.get(0)) {
//                JSONObject jsonObject=new JSONObject();
//
//                jsonObject.put("openId",expendRecord.getOpenId());
//                jsonObject.put("expendMoney",expendRecord.getExpendMoney());
//                jsonObject.put("expendType",expendRecord.getExpendType());
//                jsonObject.put("expendTime",expendRecord.getExpendTime());
//                erArray.add(jsonObject);
//
//            }
//
//        }


//        JSONObject jsonObject=new JSONObject();
//        jsonObject.put("rechargeValue",reArray);
//        jsonObject.put("expendValue",erArray);
//
//        //用户踢球获得的广告金记录
//      // JSONObject jsonKic= getKickBallByOpenId(openId);
//       // jsonObject.put("kickballValue",jsonKic);
//
//        return jsonObject;
//    }






    // 获取系统设置
    @Override
    public JSONObject getSystemSetting() {
         SystemSetting systemSetting= iSystemSettingDAO.getSystemZero();
         if(systemSetting==null)
             return null;

         JSONObject jsonObject=new JSONObject();
         jsonObject.put("id",systemSetting.getId());
         jsonObject.put("updateNotice",systemSetting.getUpdateNotice());
         jsonObject.put("gamePlay",systemSetting.getGamePlay());
         jsonObject.put("kickSkill",systemSetting.getKickSkill());
        return jsonObject;
    }

    //修改系统设置
    @Override
    public JSONObject updateSystemSetting(SystemSetting systemSetting) {

       int result= iSystemSettingDAO.updateSystemSetting(systemSetting);
        if(result >0){
            return getSystemSetting();
        }
        return null;
    }

    @Override
    public FbTip getFbTip() {

        return  iFbTipDAO.getFbTip();
    }

    @Override
    public boolean saveFbTip(FbTip fbTip) {
       long resu= (long) iFbTipDAO.save(fbTip);
       if(resu>0)
        return true;
        return false;
    }

    @Override
    public CommonProblem addCommonProblem(CommonProblem commonProblem) {

        return iCommonProblemDAO.get(iCommonProblemDAO.save(commonProblem));
    }

    @Override
    public JSONObject getCommonProblem() {
       List list= iCommonProblemDAO.getCommonProblem();
       if(list==null)
           return null;

        JSONArray erArray=new JSONArray();
        for (CommonProblem commonProblem:(List<CommonProblem>)list) {
            JSONObject jsonObject=new JSONObject();

            jsonObject.put("id",commonProblem.getId());
            jsonObject.put("problemTitle",commonProblem.getProblemTitle());
            jsonObject.put("getProblemContent",commonProblem.getGetProblemContent());
            jsonObject.put("createTime",commonProblem.getCreateTime());
            erArray.add(jsonObject);

        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("value",erArray);
        return jsonObject;
    }

    @Override
    public Boolean setSwitch(long status) {
          Switch sw= iSwitchDAO.get(1L);
          sw.setStatus(status);

        return  iSwitchDAO.update(sw);
    }

    @Override
    public Switch getSwitch() {
        return iSwitchDAO.get(1L);
    }


    @Override
    public Boolean setAliPaySwitch(long status) {
        AliPaySwitch aliPaySwitch=iAliPaySwitchDAO.get(1L);
        aliPaySwitch.setStatus(status);
        return iAliPaySwitchDAO.update(aliPaySwitch);
    }

    @Override
    public AliPaySwitch getAliPaySwitch() {
        return iAliPaySwitchDAO.get(1L);
    }
}




























